<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<script>
    function Queue(){
        this.data=[];
    }
    Queue.prototype={
        constructor:Queue,
        enqueue: function (elm) {//入队
            this.data.push(elm);
        },
        dequeue: function () {//出队
            return this.data.shift();
        },
        front: function () {//获取队首元素
            return this.data[0];
        },
        back: function () {//获取队尾元素
            return this.data[this.length-1];
        },
        clear: function () {//清空
            delete this.data;
            this.data=[];
        },
        isEmpty: function () {//是否为空
            return this.data.length==0?true:false;
        },
        count: function () {
            return this.data.length;
        },
        toString: function () {//显示
            return this.data.join('\n');
        }
    }

    var persons=[
        {name:'Allsion',sex:'F'},
        {name:'Frank',sex:'M'},
        {name:'Mason',sex:'M'},
        {name:'Calyton',sex:'M'},
        {name:'Cheryl',sex:'F'},
        {name:'Raymond',sex:'M'},
        {name:'Jennifter',sex:'F'},
        {name:'Bryan',sex:'M'},
        {name:'David',sex:'M'},
        {name:'Danny',sex:'M'},
        {name:'Aurora',sex:'F'}
    ];

    var males=new Queue(),females=new Queue();
    for(var l=persons.length,person;--l;){
        person=persons[l];
        if('M'==person.sex){
            males.enqueue(person);
        }else{
            females.enqueue(person);
        }
    }
    console.log(males,females);

    function dance(males,females){
        var person;
        while(!males.isEmpty()&&!females.isEmpty()){
            console.info('The dance partners are:');
            person=females.dequeue();
            console.log('Female dancer is:',person.name);
            person=males.dequeue();
            console.log('and male dancer is:',person.name);
        }
        if(!females.isEmpty()){
            console.log(females.front().name,' is waiting to dance.');
        }
        if(!males.isEmpty()){
            console.log(males.front().name,' is waiting to dance.');
        }
    }
    dance(males,females);

</script>
</body>
</html>